package cn.com.computer.my.computer.music.service;

import cn.com.computer.common.base.bean.CandyResult;
import cn.com.computer.common.enums.StatusEnum;
import cn.com.computer.common.utils.helper.ParamUtils;
import cn.com.computer.my.computer.music.dal.domain.MusicDO;
import cn.com.computer.my.computer.music.dal.mapper.MusicMapper;
import cn.com.computer.my.computer.music.web.bean.MusicBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by WANGXin on 2017/5/9.
 */
@Component
public class MusicService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MusicService.class);

    private static final Integer ZERO = 0;
    private static final String CREATE_NAME = "WANGXin";

    @Autowired
    private MusicMapper musicMapper;


    public CandyResult save(MusicBean musicBean){
        CandyResult candyResult = new CandyResult();

        // 1 check data
        if (musicBean == null){
            candyResult.setMessage("音乐信息不能为空");
            return candyResult;
        }
        if (StringUtils.isEmpty(musicBean.getMusicName())){
            candyResult.setMessage("音乐名称不能为空");
            return candyResult;
        }
        if (StringUtils.isEmpty(musicBean.getMusicLanguage())){
            candyResult.setMessage("音乐语言不能为空");
            return candyResult;
        }
        if (StringUtils.isEmpty(musicBean.getMusicCapacity())){
            candyResult.setMessage("音乐大小不能为空");
            return candyResult;
        }
        if (StringUtils.isEmpty(musicBean.getMusicQuality())){
            candyResult.setMessage("音乐品质不能为空");
            return candyResult;
        }
        if (StringUtils.isEmpty(musicBean.getMusicFormat())){
            candyResult.setMessage("音乐格式不能为空");
            return candyResult;
        }

        // 2 data preProcess
        MusicDO checkMusicDO = new MusicDO();
        checkMusicDO.setMusicName(musicBean.getMusicName());
        checkMusicDO.setMusicAuthor(musicBean.getMusicAuthor());
        List<MusicDO> musicDOListInDataBase;
        try {
            musicDOListInDataBase = musicMapper.queryMusicByCondition(checkMusicDO);
        }catch (Exception e){
            LOGGER.error("查询所有音乐信息异常，异常数据为:["  + ParamUtils.serializable(checkMusicDO) + "]",e);
            candyResult.setMessage("查询所有音乐信息失败");
            return candyResult;
        }
        if (!CollectionUtils.isEmpty(musicDOListInDataBase)){
            candyResult.setMessage("数据库已经有了该音乐，保存音乐失败");
            return candyResult;
        }

        MusicDO musicDO = new MusicDO();
        musicDO.setMusicName(musicBean.getMusicName());
        musicDO.setMusicAuthor(musicBean.getMusicAuthor());
        musicDO.setMusicLanguage(musicBean.getMusicLanguage());
        musicDO.setMusicCapacity(musicBean.getMusicCapacity());
        musicDO.setMusicQuality(musicBean.getMusicQuality());
        musicDO.setMusicFormat(musicBean.getMusicFormat());
        musicDO.setStatus(StatusEnum.YES.getCode());
        musicDO.setCreateBy(CREATE_NAME);

        // 3 data algorithm

        Integer saveMusicCount;
        try {
            saveMusicCount = musicMapper.saveMusic(musicDO);
        }catch (Exception e){
            LOGGER.error("保存音乐信息异常，异常数据为:["  + ParamUtils.serializable(musicDO) + "]",e);
            candyResult.setMessage("保存音乐信息失败");
            return candyResult;
        }
        if (saveMusicCount.equals(ZERO)){
            LOGGER.error("保存音乐信息异常，异常数据为:["  + ParamUtils.serializable(musicDO) + "]");
            candyResult.setMessage("保存音乐信息失败");
            return candyResult;
        }

        candyResult.setSuccess(true);
        return candyResult;
    }


}
